package sisi.cash;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.ComboitemRenderer;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import sisi.General;
import sisi.invoices.Custinvoice;
import sisi.invoices.CustinvoiceController;
import sisi.invoices.Suppinvoice;
import sisi.invoices.SuppinvoiceController;
import sisi.payment.Paymentmode;
import sisi.payment.PaymentsmodeController;

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class CashWindow extends Window implements 
				org.zkoss.zk.ui.ext.AfterCompose {
//window
private Window windowCash, windowEditCash;
private Textbox tbCerca;
private Listbox boxCash;
private List lista_Cash;
private boolean lNuovo;
private Toolbarbutton buttonSeleziona;
private ListModelList listModelList;
private Integer idSelezionato = 0;

protected Map<String, Object> args;
private Object CashItem;
private Cash modCash;
// edit
private Textbox note;
private Textbox ref;
private Intbox cashid;
private Doublebox calcamount;
private Datebox ccdebitdt;
private Datebox ccexpyredt;
private Textbox ccnumber;
private Doublebox ccpayamount;
private Datebox ccpaydt;
private Textbox cctype;
private Textbox chbank;
private Doublebox chpayamount;
private Datebox chpaycurrdt;
private Datebox chpaydate;
private Textbox chref;
private Checkbox check;
private Doublebox csamount;
private Datebox csdt;
private Textbox curr;
private Intbox custid;
private Textbox doctype;
private Datebox dtup;
private Doublebox fiamount;
private Datebox fiexpyredt;
private Checkbox fiexpyred;
private Textbox firef;
private Datebox fistartdt;
private Doublebox fxrate;
private Textbox invref;
private Combobox paymode;
private Doublebox rbamount;
private Datebox rbdate;
private Datebox rbexpyredt;
private Intbox suppid;
private Doublebox wtamount;
private Datebox wtdt;
private Datebox wtpaycurrdt;
private Bandbox bbRecNum;
private Listbox boxAllRecords;

// parameters
private String parDocType = "";
private String parRef = "";
private String parInvref = "";
private Integer idInvoiceRecNum = 0;
private Integer valoreOpzioneMenu = 0;
private Custinvoice ci;
private Suppinvoice si;
// 
private List<Paymentmode> listPaymentMode;
private ListModelList listModelListPaymentMode;
private String selectedPaycode = "";
private Integer currentPaymentNumber = 1;
private Label lblPreRecords, lblNumRecords, lblPostRecords;


		public void onCreate$windowCash(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		String tastoSeleziona = (String) args.get("tastoSeleziona");
		tastoSeleziona = (tastoSeleziona == null || tastoSeleziona.isEmpty() ? "N": tastoSeleziona);
		boolean lTastoSeleziona = tastoSeleziona.equals("S");
		if (!lTastoSeleziona) {
			buttonSeleziona.setVisible(false);
		}
		idSelezionato = (Integer) args.get("idselezionato");

		listModelList = new ListModelList();
		lista_Cash = new CashController().getList(null);
		listModelList.addAll(lista_Cash);
		boxCash.setModel(listModelList);
		boxCash.setItemRenderer(new CashItemRenderer());

		if (idSelezionato!=null && idSelezionato!=0){
		for (int i = 0; i < lista_Cash.size(); i++) {
			Cash record = (Cash)lista_Cash.get(i);
			if (record.getCashId()==idSelezionato.intValue()){
				boxCash.setSelectedIndex(i);
				break;
			}
		}
	}

}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	public void onOK$tbCerca(Event event){
		cerca();
	}

	public void onClick$buttonCerca(Event event) {
		cerca();
	}


	private void cerca(){
		listModelList = new ListModelList();
		lista_Cash = new CashController().getList(tbCerca.getValue());
		listModelList.addAll(lista_Cash);
		boxCash.setModel(listModelList);
		boxCash.setItemRenderer(new CashItemRenderer());
	}

	public void onDoubleClicked(Event event) {
		if (buttonSeleziona.isVisible()) {
			seleziona();
		} else {
			modifica(false);
		}
	}

	public void onClick$buttonSeleziona(Event event) {

		seleziona();

}

	public void seleziona() {
		if (boxCash.getSelectedIndex() >= 0 && buttonSeleziona.isVisible()) {
			Object oCash = boxCash.getSelectedItem().getAttribute("rigaCash");
			String codiceSelezionato = ""+((Cash) oCash).getCashId();
			Sessions.getCurrent().setAttribute("codiceCashSelezionato", codiceSelezionato);
			windowCash.detach();
		}
	}

	public void onClick$buttonNuovo(Event event) {
		modifica(true);
	}

	public void onClick$buttonModifica(Event event) {
		modifica(false);
	}

	private void modifica(boolean nuovo){
		try {

			int nIndex = boxCash.getSelectedIndex();

			if (!nuovo && nIndex == -1) { // nessuna riga selezionata

				Messagebox.show("Select a row to modify", "Information", Messagebox.OK, Messagebox.INFORMATION);

			} else {
				HashMap map = new HashMap();
				if (nuovo){
					map.put("lNuovo", true);
				}
				map.put("boxCash", boxCash);
				Window finestraEditCash = (Window) Executions.createComponents("/editCash.zul", null, map);
				finestraEditCash.doModal();
			}

		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		}
	}

	public void onClick$buttonCancella(Event event) {
	// TODO Auto-generated method stub

	}

	public class CashItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {
			new Listcell(""+((Cash) data).getCashId()).setParent(li);
			new Listcell(""+((Cash) data).getInvref()).setParent(li);
			li.setAttribute("rigaCash", data);
			ComponentsCtrl.applyForward(li, "onDoubleClick=onDoubleClicked");
		}
	}


// EDIT: NEW/MODIFY
	public void onCreate$windowEditCash(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		
		valoreOpzioneMenu = (Integer) args.get("valoreOpzioneMenu");
		idInvoiceRecNum = (Integer) args.get("idInvoiceRecNum");
		parInvref = (String) args.get("invref");
		parRef = (String) args.get("ref");
		parDocType = (String) args.get("docType"); // AC: attive, credit PD: passive, debit
		ci = (Custinvoice) args.get("ci");
		si = (Suppinvoice) args.get("si");

		setFieldValues();

		
	}

	private void setFieldValues(){

		List<Cash> cashList = new CashController().getList(parInvref, parRef, parDocType);
		
		lNuovo = false;
		String sPaymentMode = "WT";
		
		if (cashList.size()==0){
			lNuovo = true;
			modCash = new Cash();
			bbRecNum.setValue("");
		} else {
			boolean trovato = false;
			if (idInvoiceRecNum!=null){
				for (int i = 0; i < cashList.size(); i++) {
					Cash cash = (Cash) cashList.get(i);
					if (cash.getCashId().equals(idInvoiceRecNum)){
						modCash = cashList.get(i);
						bbRecNum.setValue(""+modCash.getCashId());
						trovato = true;
						break;
					}
				}
			}
			if (!trovato && idInvoiceRecNum != null){
				modCash = cashList.get(0);
				bbRecNum.setValue(""+modCash.getCashId());
			} else if (!trovato && idInvoiceRecNum == null ) {
				modCash = new Cash();
				modCash.setPaymode("WT"); // default
				lNuovo = true;
				bbRecNum.setValue("");
			}
			sPaymentMode = modCash.getPaymode();
		}
		CashItem = modCash;
			
		listPaymentMode = new PaymentsmodeController().getPaymentmodeList(null);
		listModelListPaymentMode = new ListModelList();
		listModelListPaymentMode.addAll(listPaymentMode);
		paymode.setModel(listModelListPaymentMode);
		paymode.setItemRenderer(new PaymodeItemRenderer());
		
		paymode.setWidth("50px");
		paymode.setReadonly(true);
		
		Integer iSelected = 0;
		for (int i = 0; i < listModelListPaymentMode.size(); i++) {
			Paymentmode pm = (Paymentmode) listModelListPaymentMode.get(i);
			if (pm.getPaycode().equalsIgnoreCase(sPaymentMode)){
				iSelected = i;
				break;
			}
		}
		
		if (listModelListPaymentMode.size()>0){
			listModelListPaymentMode.addToSelection(listModelListPaymentMode.get(iSelected));
			paymode.setValue(((Paymentmode)listModelListPaymentMode.get(iSelected)).getPaycode());
		}

		selectedPaycode = paymode.getValue();

		
		ListModelList listModelList = new ListModelList();

		listModelList.addAll(cashList); 
		boxAllRecords.setModel(listModelList);
		boxAllRecords.setItemRenderer(new AllPayment() );
		
		int numRecords = listModelList.getSize();
		lblPreRecords.setValue("This reference contains ");
		lblNumRecords.setValue(" "+numRecords+" ");
		lblPostRecords.setValue("Record" + (numRecords==1?"":"s"));
		
		
		if (cashList!=null && cashList.size()>0){
			//bbCurRecNum.setValue(""+invoiceAttuale);
			//boxAllRecords.setSelectedIndex((invoiceAttuale-1));
		}

				
		if (!lNuovo) {
			windowEditCash.setTitle("Edit Cash");
			note.setValue(modCash.getNote());
			ref.setValue(modCash.getRef());
			cashid.setValue(modCash.getCashId());
			calcamount.setValue(modCash.getCalcAmount());
			ccdebitdt.setValue( (java.util.Date) (modCash.getCcDebitDt()));
			ccexpyredt.setValue( (java.util.Date) (modCash.getCcExpyredt()));
			ccnumber.setValue(modCash.getCcNumber());
			ccpayamount.setValue(modCash.getCcPayAmount());
			ccpaydt.setValue( (java.util.Date) (modCash.getCcPayDt()));
			cctype.setValue(modCash.getCcType());
			chbank.setValue(modCash.getChBank());
			chpayamount.setValue(modCash.getChPayAmount());
			chpaycurrdt.setValue( (java.util.Date) (modCash.getChPayCurrDt()));
			chpaydate.setValue( (java.util.Date) (modCash.getChPayDate()));
			chref.setValue(modCash.getChRef());
			check.setChecked(modCash.getCheck());
			csamount.setValue(modCash.getCsAmount());
			csdt.setValue( (java.util.Date) (modCash.getCsDt()));
			curr.setValue(modCash.getCurr());
			custid.setValue(modCash.getCustid());
			doctype.setValue(modCash.getDoctype());
			dtup.setValue( (java.util.Date) (modCash.getDtUp()));
			fiamount.setValue(modCash.getFiAmount());
			fiexpyredt.setValue( (java.util.Date) (modCash.getFiExpyreDt()));
			fiexpyred.setChecked(modCash.getFiExpyred());
			firef.setValue(modCash.getFiRef());
			fistartdt.setValue( (java.util.Date) (modCash.getFiStartDt()));
			fxrate.setValue(modCash.getFxRate());
			invref.setValue(modCash.getInvref());
			//paymode.setValue(modCash.getPaymode());
			rbamount.setValue(modCash.getRbAmount());
			rbdate.setValue( (java.util.Date) (modCash.getRbDate()));
			rbexpyredt.setValue( (java.util.Date) (modCash.getRbExpyreDt()));
			suppid.setValue(modCash.getSuppid());
			wtamount.setValue(modCash.getWtAmount());
			wtdt.setValue( (java.util.Date) (modCash.getWtDt()));
			wtpaycurrdt.setValue( (java.util.Date) (modCash.getWtPayCurrDt()));
			
			setWritableFields(0, iSelected);
			
		} else {
			windowEditCash.setTitle("New Cash");
			doctype.setValue(parDocType);
			invref.setValue(parInvref);
			ref.setValue(parRef);
			dtup.setValue( new java.util.Date());
			calcamount.setValue(0);
			
			if (ci!=null && ci.getRecnum()!=null){
				custid.setValue(ci.getCustid());
				suppid.setValue(ci.getSuppid());
				curr.setValue(ci.getCurr());
				fxrate.setValue(ci.getExchangerate());
			} else {
				custid.setValue(si.getCustid());
				suppid.setValue(si.getSuppid());
				curr.setValue(si.getCurr());
				fxrate.setValue(si.getExchangerate());
			}

			dtup.setValue( new java.util.Date() );
			check.setChecked(false);			
			
			setWritableFields(1, iSelected);
			
		}


		
		
	}
	
	public void onSelect$boxAllRecords(){
		
		ListModel<Cash> lml = boxAllRecords.getListModel();
		
		Cash cash = (Cash) ((List) lml).get(boxAllRecords.getSelectedIndex());
		
		//valoreOpzioneMenu = (Integer) args.get("valoreOpzioneMenu");
		idInvoiceRecNum = cash.getCashId();
		parInvref = cash.getInvref();
		parRef = cash.getRef();
		parDocType = cash.getDoctype(); 
		
		fxrate.setValue(cash.getFxRate());

		setFieldValues();	
		
	}
	
	public void onSelect$paymode(){
		setWritableFields(1, null);
	}

	
	private void totals(){
		Double total = 0.00;
		total += ccpayamount.getValue();
		total += chpayamount.getValue();
		total += fiamount.getValue();
		total += wtamount.getValue();
		total += rbamount.getValue();
		total += csamount.getValue();
		
		calcamount.setValue( total );
		
	}
	
	private void setWritableFields(Integer option, Integer iSelected){
		
		
		//int selected = paymode.getSelectedIndex();
		int selected = paymodeSelected();
		if (option==0){
			selected = iSelected;
		} else if (option==1 && iSelected != null){
			selected = iSelected;
		}
		
		if (selected == -1){
			return;
		}
		
		
		if (option==1){
			cctype.setValue("");
			ccnumber.setValue("");
			ccexpyredt.setValue(null);
			ccpaydt.setValue(null);
			ccdebitdt.setValue(null);
			ccpayamount.setValue(0);


			// check 
			chref.setValue("");
			chbank.setValue("");
			chpaydate.setValue(null);
			chpaycurrdt.setValue(null);
			chpayamount.setValue(0);
			
			// financing
			firef.setValue("");
			fistartdt.setValue(null);
			fiexpyredt.setValue(null);
			fiamount.setValue(0);
			fiexpyred.setChecked(false);
			
			// wire transfert
			wtdt.setValue(null);
			wtpaycurrdt.setValue(null);
			wtamount.setValue(0);
			
			// cash order
			rbdate.setValue(null);
			rbexpyredt.setValue(null);
			rbamount.setValue(0);

			// cash
			csdt.setValue(null);
			csamount.setValue(0);
			
		}
		
		Paymentmode pm = (Paymentmode) listModelListPaymentMode.get(selected);
		
		boolean roWT = true, roCH = true, roCC = true, roCS = true, roRB = true, roFI = true;
		
		if (pm.getPaycode().equalsIgnoreCase("WT")){
			roWT = false;
		} else if (pm.getPaycode().equalsIgnoreCase("CH")){
			roCH = false;
		} else if (pm.getPaycode().equalsIgnoreCase("CC")){
			roCC = false;
		} else if (pm.getPaycode().equalsIgnoreCase("CS")){
			roCS = false;
		} else if (pm.getPaycode().equalsIgnoreCase("RB")){
			roRB = false;
		} else if (pm.getPaycode().equalsIgnoreCase("FI")){
			roFI = false;
		}
		
		// credit card
		cctype.setReadonly(roCC);
		ccnumber.setReadonly(roCC);
		ccexpyredt.setDisabled(roCC);
		ccpaydt.setDisabled(roCC);
		ccdebitdt.setDisabled(roCC);
		ccpayamount.setReadonly(roCC);

		// check
		chref.setReadonly(roCH);
		chbank.setReadonly(roCH);
		chpaydate.setDisabled(roCH);
		chpaycurrdt.setDisabled(roCH);
		chpayamount.setDisabled(roCH);
		
		// financing
		firef.setReadonly(roFI);
		fistartdt.setDisabled(roFI);
		fiexpyredt.setDisabled(roFI);
		fiamount.setReadonly(roFI);
		fiexpyred.setDisabled(roFI);
		
		// wire transfert
		wtdt.setDisabled(roWT);
		wtpaycurrdt.setDisabled(roWT);
		wtamount.setReadonly(roWT);
		
		// cash order
		rbdate.setDisabled(roRB);
		rbexpyredt.setDisabled(roRB);
		rbamount.setReadonly(roRB);

		// cash
		csdt.setDisabled(roCS);
		csamount.setReadonly(roCS);
		

		
	}
	
	
	public void onClick$btnAnnulla(Event event){ 
		windowEditCash.detach();
	}

	public void onClick$btnConferma(Event event){
		if (lNuovo){
			modCash = new Cash();
		} else {

		}
		String testoMsg = ""; 
		
		//Integer pmSelectedIndex = paymode.getSelectedIndex();
		Integer pmSelectedIndex = paymodeSelected();
		if (pmSelectedIndex==null || pmSelectedIndex<0){
			return;
		}
		
		totals();
		
		
		// TODO controllare la somma pagata con l'importo della fattura e dare un avviso.
		
		Paymentmode paymentMode = (Paymentmode) listPaymentMode.get(pmSelectedIndex);

		modCash.setNote(note.getValue());
		modCash.setRef(ref.getValue());
		modCash.setCashId(cashid.getValue());
		modCash.setCalcAmount(calcamount.getValue());
		modCash.setCcDebitDt( (ccdebitdt.getValue() != null ? sisi.General.date2SqlTimestamp(ccdebitdt.getValue()): null) );
		modCash.setCcExpyredt( (ccexpyredt.getValue() != null ? sisi.General.date2SqlTimestamp(ccexpyredt.getValue()): null) );
		modCash.setCcNumber(ccnumber.getValue());
		modCash.setCcPayAmount(ccpayamount.getValue());
		modCash.setCcPayDt( (ccpaydt.getValue() != null ? sisi.General.date2SqlTimestamp(ccpaydt.getValue()): null) );
		modCash.setCcType(cctype.getValue());
		modCash.setChBank(chbank.getValue());
		modCash.setChPayAmount(chpayamount.getValue());
		modCash.setChPayCurrDt( (chpaycurrdt.getValue() != null ? sisi.General.date2SqlTimestamp(chpaycurrdt.getValue()): null) );
		modCash.setChPayDate( (chpaydate.getValue() != null ? sisi.General.date2SqlTimestamp(chpaydate.getValue()): null) );
		modCash.setChRef(chref.getValue());
		modCash.setCheck(check.isChecked());
		modCash.setCsAmount(csamount.getValue());
		modCash.setCsDt( (csdt.getValue() != null ? sisi.General.date2SqlTimestamp(csdt.getValue()): null) );
		modCash.setCurr(curr.getValue());
		modCash.setCustid(custid.getValue());
		modCash.setDoctype(doctype.getValue());
		modCash.setDtUp( (dtup.getValue() != null ? sisi.General.date2SqlTimestamp(dtup.getValue()): null) );
		modCash.setFiAmount(fiamount.getValue());
		modCash.setFiExpyreDt( (fiexpyredt.getValue() != null ? sisi.General.date2SqlTimestamp(fiexpyredt.getValue()): null) );
		modCash.setFiExpyred(fiexpyred.isChecked());
		modCash.setFiRef(firef.getValue());
		modCash.setFiStartDt( (fistartdt.getValue() != null ? sisi.General.date2SqlTimestamp(fistartdt.getValue()): null) );
		modCash.setFxRate(fxrate.getValue());
		modCash.setInvref(invref.getValue());
		modCash.setPaymode(paymode.getValue());
		modCash.setRbAmount(rbamount.getValue());
		modCash.setRbDate( (rbdate.getValue() != null ? sisi.General.date2SqlTimestamp(rbdate.getValue()): null) );
		modCash.setRbExpyreDt( (rbexpyredt.getValue() != null ? sisi.General.date2SqlTimestamp(rbexpyredt.getValue()): null) );
		modCash.setSuppid(suppid.getValue());
		modCash.setWtAmount(wtamount.getValue());
		modCash.setWtDt( (wtdt.getValue() != null ? sisi.General.date2SqlTimestamp(wtdt.getValue()): null) );
		modCash.setWtPayCurrDt( (wtpaycurrdt.getValue() != null ? sisi.General.date2SqlTimestamp(wtpaycurrdt.getValue()): null) );

		if (lNuovo){
			modCash = new CashController().add(modCash);
			testoMsg = "New Cash: "+ General.trimNull( modCash.getInvref() );
			cashid.setValue(modCash.getCashId());
			idInvoiceRecNum = modCash.getCashId();
			parInvref = modCash.getInvref();
			parRef = modCash.getRef();
			parDocType = modCash.getDoctype();
			lNuovo = false;
		} else {
			new CashController().update(modCash);
			testoMsg = "Updating Cash: "+ General.trimNull( modCash.getInvref() );
		}

		
		// sommare il totale dei pagamenti da salvare su customer invoice o supplier invoice 	
		
		Double totpay = totPay(parInvref, parRef, parDocType);
		if (ci!=null && ci.getRecnum()!=null){
			ci.setPaidamount(totpay);
			new CustinvoiceController().update(ci);
		} else { 
			si.setPaidamount(totpay);
			new SuppinvoiceController().update(si);
		}
		
		setFieldValues();
		
		General.showNotify( testoMsg, windowEditCash);

		//windowEditCash.detach();

	}

	private Double totPay( String parInvref, String parRef, String parDocType ){
		List<Cash> cashList = new CashController().getList(parInvref, parRef, parDocType);
		Double totpay = 0.00;
		for (Cash cash : cashList) {
			totpay += cash.getCalcAmount();
		}
		return totpay;
	}
	
	private class PaymodeItemRenderer implements ComboitemRenderer {
		
		@Override
		public void render(Comboitem item, Object data, int index)
				throws Exception {
			
			Paymentmode paymentmode = (Paymentmode)data;
			item.setDescription(""+paymentmode.getPayid() + " " + paymentmode.getPaymode());
			item.setLabel(""+paymentmode.getPaycode());			
		}


	}

	
	
	
	private class AllPayment implements ListitemRenderer {
		
		@Override
		public void render(Listitem item, Object data, int index)
				throws Exception {
			
			Cash ci = (Cash)data;
			
			new Listcell(""+ci.getCashId()).setParent(item);
			//new Listcell(""+General.formatoFecha( ci.getInvoicedt() ) ).setParent(item);
			new Listcell(""+ci.getDoctype() ).setParent(item);
			//new Listcell(""+General.decimalFormat( "#,###,##0.00", ci.getTotalinvoice() ) ).setParent(item);
			new Listcell(""+ci.getInvref() ).setParent(item);
			new Listcell(""+ci.getPaymode() ).setParent(item);
			new Listcell(""+General.decimalFormat( "#,###,##0.00", ci.getCalcAmount() ) ).setParent(item);
			item.setAttribute("rigaAllPayments", data);
		}
	}

	public void onClick$btnNewRecord(){
		idInvoiceRecNum = null;
		cashid.setValue(0);
		setFieldValues();
	}

	public void onClick$btnDeleteRecord(){

		if (cashid.getValue()==null || cashid.getValue().equals(Integer.parseInt("0"))){
			windowEditCash.detach();
			return;
		}

		if (Messagebox.show("Do you really want to delete current record?",
				"NFYS", Messagebox.YES | Messagebox.NO,
				Messagebox.QUESTION) != Messagebox.YES){
			return;
		}

		try {
			Cash cash = new CashController().getxId(idInvoiceRecNum);
			new CashController().remove(cash);
			General.showNotify("Payment "+idInvoiceRecNum+" deleted!!!", null, 6000);
		} catch (Exception e) {
			General.showNotify("Error deleting payment id: "+idInvoiceRecNum, null, 6000, "error");
		}
		windowEditCash.detach();
	}

	
	private Integer paymodeSelected(){
		
		Integer iSelected = -1;
		String sPaymentMode = paymode.getValue();
		for (int i = 0; i < listModelListPaymentMode.size(); i++) {
			Paymentmode pm = (Paymentmode) listModelListPaymentMode.get(i);
			if (pm.getPaycode().equalsIgnoreCase(sPaymentMode)){
				iSelected = i;
				break;
			}
		}
		
		if (listModelListPaymentMode.size()>0){
			listModelListPaymentMode.addToSelection(listModelListPaymentMode.get(iSelected));
			paymode.setValue(((Paymentmode)listModelListPaymentMode.get(iSelected)).getPaycode());
		}
		
		return iSelected;
	}
	
}
